﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM
{
    public partial class ProvincePopup : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            LoadBP();
            Timer1.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = this.grdBP.SelectedRow;
                DataTable dt = (DataTable)Session["ProvinceTable"];
                if (dt != null)
                {
                    int index = row.DataItemIndex;
                    var data = dt.Rows[index];
                    if (data != null)
                    {
                        Session["ChosenCode"] = data["Code"];
                        Session["ChosenName"] = data["Name"];
                        //ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectBP');", true);
                    }
                }
            }
            catch (Exception ex)
            {

            }

            //this.grdBP.PageIndex = e.NewPageIndex;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 
                    e.Row.Attributes["style"] = "cursor:pointer";
                    e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdBP, "Select$" + e.Row.RowIndex);
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tblProvince = (DataTable)Session["ProvinceTable"];
                if (tblProvince != null && this.txtSearch.Text.Trim().Length > 0)
                {
                    DataView dv = tblProvince.DefaultView;

                    dv.RowFilter = "Code like '%" + this.txtSearch.Text.Trim() + "%' Or Name like '%" + this.txtSearch.Text.Trim() + "%'";

                    this.grdBP.DataSource = dv.ToTable();
                    this.grdBP.DataBind();
                }
            }
            catch (Exception ex)
            { 
            
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow selectedRow = this.grdBP.SelectedRow;
                if (selectedRow == null) return;
                Session["ChosenProvince"] = selectedRow;
                Session["ProvinceTable"] = null;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('SelectProvince');", true);
            }
            catch (Exception ex)
            {

            }
        }

        private void LoadBP()
        {
            try
            {
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                string SQLCommand = "select * from [@PROVINCECODE] order by cast(Code as int) ";
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                DataTable dtProvince = GF.GetMasterListBySqlCommand(dbType, SQLCommand);
                BindBP(dtProvince);
            }
            catch (Exception ex)
            { 
            
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBP(DataTable tblProvince)
        {
            Session["ProvinceTable"] = tblProvince;
            this.grdBP.DataSource = tblProvince;
            this.grdBP.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DataTable tblProvince = (DataTable)Session["ProvinceTable"];
            this.grdBP.PageIndex = e.NewPageIndex;
            BindBP(tblProvince);
        }
    }
}